﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FrameManager
{
    static public class MachineStatus
    {
        static public bool bAuto = false;
        static public bool bPrepareAuto = false;
        static public bool bStart = false;
        static public bool bAlarm = false;
        static public bool bSafety = false;
        static public bool bBizzDisable = false;
        static public bool bHomeed = false;
        static public bool bEstop = false;
        static public bool bSemiAuto = false;
        static public bool bRemaid = false;
        static public bool bEmptyRun = false;
        static public bool bByPass = false;
        static public bool bHoming = false;
        static public bool bDoorOpen = false;
        static public bool bLightSenser = false;
        static public bool Is4Axis = false;

        public delegate bool SetMachineStart();
        static public event SetMachineStart setMachineStart;
        public delegate bool SetMachineStop();
        static public event SetMachineStop setMachineStop;
        public delegate bool SetMachineAlarm();
        static public event SetMachineAlarm setMachineAlarm;
        public delegate bool ResetMachineAlarm();
        static public event ResetMachineAlarm resetMachineAlarm;
        public delegate bool SetMachineEstop();
        static public event SetMachineEstop setMachineEstop;
        public delegate bool ResetMachineEstop();
        static public event ResetMachineEstop resetMachineEstop;
        public delegate bool SetHomeStart();
        static public event SetHomeStart setHomeStart;
        public delegate bool SetDoorOpen();
        static public event SetDoorOpen setDoorOpen;
        public delegate bool ResetDoorOpen();
        static public event ResetDoorOpen resetDoorOpen;
        public delegate bool SetLightSenser();
        static public event SetLightSenser setLightSenser;
        public delegate bool ResetLightSenser();
        static public event ResetLightSenser resetLightSenser;
        static public void SetMachineStartAction()
        {
            if (bHomeed == false)
                return;
            if (bEstop)
                return;
            if (bDoorOpen)
                return;
            if (bAlarm)
                return;
            if (bLightSenser)
                return;
            if (setMachineStart != null)
            {
                if (setMachineStart()==false)
                    return;
            }
            Action action = () =>
              {
                  if (FrameManager.FrmStart != null)
                  {
                      //if (FrameManager.FrmStart.Visible)
                      //    return;
                      FrameManager.ShowForm(FrameManager.FrmStart);
                  }
                  
              };
            FrameManager.fluentDesignFormContainer1.Invoke(action);
            bAuto = true;
            bStart = true;
        }
        
       
        static public void SetMachineStopAction()
        {
            if (setMachineStop != null)
            {
                setMachineStop();
            }
            bHoming = false;
            bSemiAuto = false;
            bStart = false;
            bAuto=false;
        }
        static public void SetMachineAlarmAction()
        {
            if (setMachineAlarm != null)
            {
                setMachineAlarm();
            }
            bAlarm = true;
            bStart = false;
            bAuto = false;
        }
        static public void ResetMachineAlarmAction()
        {
            if (resetMachineAlarm != null)
            {
                if (resetMachineAlarm())
                {
                    //bAlarm = false;
                }
            }
            for (int i = FrameManager.flyoutPanelStack.Controls.Count - 1; i > -1; i--)
            {
                Control control = FrameManager.flyoutPanelStack.Controls[i];
                if (control is Form)
                {
                    Form form = (Form)control;
                    form.Close();
                    FrameManager.flyoutPanelStack.Controls.Remove(control);
                }
            }
            if (FrameManager.flyoutPanelStack.Controls.Count == 0)
            {
                FrameManager.flyoutPanel.HidePopup();
            }


        }
        static public void setMachineEstopAction()
        {
            if (setMachineEstop != null)
            {
                setMachineEstop();
            }
            bEstop = true;
            bHoming = false;
            bSemiAuto = false;
            bStart = false;
            bAuto = false;
        }
        static public void resetMachineEstopAction()
        {
            if (resetMachineEstop != null)
            {
                resetMachineEstop();
                bEstop = false;
                return;
            }
            bEstop = false;
        }
        static public void SetHomeStartAction()
        {
            
            if (setHomeStart != null)
            {
                setHomeStart();
            }
        }
        static public bool HaveHomeEvent()
        {
            if (setHomeStart != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        static public bool CanHomeAction()
        {
            if (bEstop)
                return false;
            if (bDoorOpen)
                return false;
            if (bAlarm)
                return false;
            if (bLightSenser)
                return false;
            return true;
        }
        static public void SetDoorOpenAction()
        {
            if (setDoorOpen != null)
            {
                setDoorOpen();
            }
            bDoorOpen = true;
        }
        static public void ResetDoorOpenAction()
        {
            if (resetDoorOpen != null)
            {
                if (resetDoorOpen())
                {
                    bDoorOpen = false;
                }
            }
        }
        static public void setLightSenserAction()
        {
            if (setLightSenser != null)
            {
                setLightSenser();
            }
            bLightSenser = true;
        }
        static public void ResetLightSenserAction()
        {
            if (resetLightSenser != null)
            {
                if (resetLightSenser())
                {
                    bLightSenser = false;
                }
            }
        }
        public static bool CanBeRun
        {
            get
            {
                if (bEstop)
                    return false;
                if (bDoorOpen)
                    return false;
                if (bAlarm)
                    return false;
                if (bLightSenser)
                    return false;
                if (bAuto || bSemiAuto||bHoming)
                {
                    return true;
                }
                else
                {
                    return false;
                }
                
            }
        }
        public static bool NoHomeAction
        {
            get
            {
                if (setHomeStart == null)
                    return true;
                else
                    return false;
            }
        }
    }
}
